package com.ihr360.excel.model.dto;

import com.google.common.collect.Maps;
import com.ihr360.commons.exception.Ihr360SpreadSheetAsynException;
import com.ihr360.commons.model.entity.StringIdWithTimeAndCompanyId;
import com.ihr360.excel.utils.SpreadSheetConvertResultUtil;
import org.apache.commons.collections.MapUtils;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.IOException;
import java.util.Map;

/**
 * 电子表格 行数据
 *
 * @author richey
 */
@Entity
@DynamicUpdate
public class CorrectSpreadsheetRow extends StringIdWithTimeAndCompanyId {

    private static final long serialVersionUID = -2879138799574402385L;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sheet_id")
    private SpreadSheet sheet;

    /**
     * 员工Id
     */
    @Column(length = 40)
    private String staffId;

    /**
     * 姓名
     */
    @Column(length = 32)
    private String staffName;

    /**
     * 手机号码
     */
    @Column(length = 18)
    private String mobileNo;

    /**
     * 行号
     */
    @Column
    private Integer rowNum;

    /**
     * 是否有权限导入
     */
    @Column
    private Boolean hasPermission = false;


    /**
     * Excel行数据内容，保存内容为表头与行数据的Map映射对象
     */
    @Column(columnDefinition = "blob")
    private byte[] data;

    public SpreadSheet getSheet() {
        return sheet;
    }

    public void setSheet(SpreadSheet sheet) {
        this.sheet = sheet;
    }

    public String getStaffId() {
        return staffId;
    }

    public void setStaffId(String staffId) {
        this.staffId = staffId;
    }

    public String getStaffName() {
        return staffName;
    }

    public void setStaffName(String staffName) {
        this.staffName = staffName;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public Integer getRowNum() {
        return rowNum;
    }

    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

    public Map<String, Object> getData() {
        if (data == null) {
            return Maps.newHashMap();
        } else {
            try {
                return (Map<String, Object>) SpreadSheetConvertResultUtil.ByteToObject(data);
            } catch (Exception e) {
                throw new Ihr360SpreadSheetAsynException(null, "导入异常，数据处理失败！");
            }
        }
    }

    public void setData(Map<String, Object> data) {
        if (MapUtils.isNotEmpty(data)) {
            try {
                this.data = SpreadSheetConvertResultUtil.ObjectToByte(data);
            } catch (IOException e) {
                throw new Ihr360SpreadSheetAsynException(null, "导入异常，数据处理失败！");
            }
        } else {
            this.data = null;
        }

    }

    public Boolean getHasPermission() {
        return hasPermission;
    }

    public void setHasPermission(Boolean hasPermission) {
        this.hasPermission = hasPermission;
    }


}